package cn.dw.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

public class BaiduMapUtil {
	@SuppressWarnings("unused")
	private static final String BaiDuMap_AK = "Mk8h4HRD1DLT3wXGM6NbmObdP0rXV70D";

	public static String point_location(Double lng, Double lat) throws Exception {
		String URI = "http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=" + lat + "," + lng
				+ "&output=json&pois=1&ak=" + "Mk8h4HRD1DLT3wXGM6NbmObdP0rXV70D";

		URL url = new URL(URI);

		BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));

		StringBuilder sb = new StringBuilder("");
		String res;
		while ((res = in.readLine()) != null) {
			sb.append(res.trim());
		}
		in.close();
		String str = sb.toString();

		int addressStart = str.indexOf("formatted_address\":");
		int addressEnd = str.indexOf(",\"business");

		String address = null;
		if ((addressStart > 0) && (addressEnd > 0)) {
			address = str.substring(addressStart + 20, addressEnd - 1);
		}
		return address;
	}

	public static void main(String[] args) throws Exception {
		String point = point_location(Double.valueOf(120.61990711999999D), Double.valueOf(31.317987370000001D));

		System.out.println(point);
	}
}
